package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;

import com.yuntongxun.ecdemo.R;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zhuonan on 2016/12/18.
 */

public class GuideActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        ButterKnife.inject(this);
    }

    MediaPlayer mediaPlayer;
    private AssetManager assetManager;

    @OnClick({R.id.button,R.id.button2,R.id.button3,R.id.button4,R.id.button5,R.id.button6,R.id.button7,R.id.button8,R.id.button10,R.id.button11})
    public void onClick(View v) {

        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.stop();
        }
        mediaPlayer=new MediaPlayer();
        assetManager = getAssets();
        switch (v.getId()) {
            case R.id.button:
                AssetFileDescriptor fileDescriptor = null;
                try {
                    fileDescriptor = assetManager.openFd("sound/london_hello_how_are_you.m4a");
                    mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),
                            fileDescriptor.getStartOffset());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button2:
                try {
                    fileDescriptor = assetManager.openFd("sound/italy_hello_how_are_you.m4a");
                    mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),
                            fileDescriptor.getStartOffset());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button3:
                try {
                    fileDescriptor = assetManager.openFd("sound/japanese_hello_how_are_you.m4a");
                    mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),
                            fileDescriptor.getStartOffset());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button4:
                try {
                    fileDescriptor = assetManager.openFd("sound/french_hello_how_are_you.m4a");
                    mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),
                            fileDescriptor.getStartOffset());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button5:
                try {
                    fileDescriptor = assetManager.openFd("sound/usa_hello_how_are_you.m4a");
                    mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),
                            fileDescriptor.getStartOffset());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button6:
                try {
                    fileDescriptor = assetManager.openFd("sound/korea_hello_how_are_you.m4a");
                    mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),
                            fileDescriptor.getStartOffset());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button7:
                try {
                    fileDescriptor = assetManager.openFd("sound/london_hello,_how_are_you.m4a");
                    mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),
                            fileDescriptor.getStartOffset());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button8:
                try {
                    fileDescriptor = assetManager.openFd("sound/chinese_hello.m4a");
                    mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),
                            fileDescriptor.getStartOffset());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button10:
                try {
                    fileDescriptor = assetManager.openFd("sound/russia_hello_nice_to_meet_you.m4a");
                    mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),
                            fileDescriptor.getStartOffset());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.button11:
                try {
                    fileDescriptor = assetManager.openFd("sound/germany_hello_how_are_you.m4a");
                    mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),
                            fileDescriptor.getStartOffset());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;

        }
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.start();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
}
